package com.douwong.student.popuwindow;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.douwong.student.R;
import com.douwong.student.utils.ScreenUtils;

import java.io.File;

/**
 * @autor yichao
 * @time 2016/12/511:44
 * @des 显示图片popuwindow
 */
public class ImageViewPopuwidow extends PopupWindow {

    private Context mContext;
    private File mFile;
    private final ImageView mImageView;
    private Bitmap mBitmap;
    public ImageViewPopuwidow(Context context, File file) {
        super(context);
        mContext=context;
        mFile = file;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mView = inflater.inflate(R.layout.show_image_popu, null);
        mImageView = (ImageView) mView.findViewById(R.id.image_pupu_anwer);
        this.setContentView(mView);
        setView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0xffffff);
        this.setBackgroundDrawable(dw);
        mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                if (!mBitmap.isRecycled()){
                    mBitmap.recycle();
                    mBitmap=null;
                    System.gc();
                }
            }
        });

    }

    /**
     * 设置数据
     */
    private void setView() {
        BitmapFactory.Options opts=new BitmapFactory.Options();
        opts.inJustDecodeBounds=true;
        BitmapFactory.decodeFile(mFile.getAbsolutePath(),opts);
        opts.inPreferredConfig = Bitmap.Config.ARGB_4444;
        opts.inSampleSize = calculateInSampleSize(opts, ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight());
        opts.inJustDecodeBounds=false;
        mBitmap = BitmapFactory.decodeFile(mFile.getAbsolutePath(),opts);
         mImageView.setImageBitmap(mBitmap);

    }
    /**
     * 计算缩放比例
     * @param op
     * @param reqWidth
     * @param reqheight
     * @return
     */
    public int calculateInSampleSize(BitmapFactory.Options op, int reqWidth,
                                     int reqheight) {
        int originalWidth = op.outWidth;
        int originalHeight = op.outHeight;
        int inSampleSize = 1;
        if (originalWidth > reqWidth || originalHeight > reqheight) {
            int halfWidth = originalWidth / 2;
            int halfHeight = originalHeight / 2;
            while ((halfWidth / inSampleSize > reqWidth)
                    &&(halfHeight / inSampleSize > reqheight)) {
                inSampleSize *= 2;

            }
        }
        return inSampleSize;
    }
}
